package ch2;

import java.util.*;

public class MapTest {

	public static void main(String[] args) {
		// Map允许null

		Map m = new HashMap();
		m.put("狗", "dog");
		m.put("猫", "cat");
		m.put("鱼", "fish");
		m.put("羊", "sheep");
		m.put("羊", "goat");
		m.put("没有", null);

		System.out.println(m.size());
		System.out.println(m);
		// remove:按照键删除，返回对应的value
		// String str = (String) m.remove("鱼");
		Scanner in = new Scanner(System.in);
		System.out.println("请输入要删除的key:");
		String key = in.next();
		if (m.containsKey(key)) {
			String value = (String) m.remove(key);
			System.out.println(key + ":" + value + "被删除了");
		} else {
			System.out.println("不在");
		}
		// 2:不保险，没有考虑到 null的情况
		// String str = (String) m.remove("没有");
		// System.out.println(str);
		// if (str == null) {
		// System.out.println("不在");
		// } else {
		// System.out.println("删除成功了。。。");
		// }
		System.out.println("删除后：" + m);
		System.out.println(">>>>>>>>>>>逐个迭代>>>>>>>>>>>>>");
		// 1:一步获得key的集合
		Set keys = m.keySet();
		// Collection c = m.values();

		// 迭代key集合
		Iterator ir = keys.iterator();
		while (ir.hasNext()) {
			Object k = ir.next();
			Object value = m.get(k);
			System.out.println(k + "=" + value);
		}
		// 2:获得key数组
	}

}
